import { isPhone, validateIsemail } from '@fl/utils/validate'
import { reactive } from 'vue'

export function useRule() {
    const validatorPhoneNumber = (rule, value, callback) => {
        if (!isPhone(value))
            callback(new Error('请输入正确的手机号'))
        else callback()
    }

    const validatorUserName = (rule, value, callback) => {
        const regex = /^[a-z_][\w-]*[a-z0-9]{4}$/i
        if (!regex.test(value))
            callback(new Error('登录名长度大于4且不包含特殊字符'))
        else callback()
    }

    // 密码校验
    const validatorPassword = (rule, value, callback) => {
        // 强密码，包含大写字母、小写字母、数字、特殊字符
        const pwdRegex = /(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{9,30}/

        if (!pwdRegex.test(value))
            callback(new Error(('密码中必须包含大小写字母、数字、特殊字符')))
        else callback()
    }

    const rules = reactive<any>({
        userName: [
            { required: true, trigger: 'blur', validator: validatorUserName },
        ],
        organizationId: [
            { message: '请选择', required: true, trigger: 'blur' },
        ],
        activeStatus: [
            { message: '请选择', required: true, trigger: 'blur' },
        ],
        email: [
            { required: true, trigger: 'blur', validator: validateIsemail },
        ],
        password: [
            { required: true, trigger: 'blur', validator: validatorPassword },
        ],
        sex: [
            { message: '请选择', required: true, trigger: 'blur' },
        ],
        phone: [
            { required: true, trigger: 'blur', validator: validatorPhoneNumber },
        ],

    })

    const userRules = reactive<any>({
        // email: [
        //     { required: true, trigger: 'blur', validator: validateIsemail },
        // ],
        enterpriseAccount: [
            { message: '请输入', required: true, trigger: 'blur' },
        ],
        phonenumber: [
            { required: true, trigger: 'blur', validator: validatorPhoneNumber },
        ],
        userName: [
            { message: '请输入', required: true, trigger: 'blur' },
        ],
    })

    return { rules, userRules }
}
